# coding=utf-8

import collections
#import random
import questions

# 真真 
# 2024-02-06
# 《四五快读》
# 《改变历史的战争》

ques = [
    ('做', 'zuò', 0),
    ('他', 'tā', 0),
    ('爱', 'ài', 0),
    ('季', 'jì', 0),
    ('都', 'dū', 0),
    ('游', 'yóu', 0),
    ('戏', 'xì', 0),
    ('跑', 'pǎo', 0),
    ('要', 'yào', 0),
    ('狗', 'gǒu', 0),
    ('猫', 'māo', 0),
    ('落', 'là', 0),
    ('孩', 'hái', 0),
    ('姐', 'jiě', 0),
    ('妹', 'mèi', 0),
    ('弟', 'dì', 0),
    ('会', 'huì', 0),
    ('着', 'zhuó', 0),
    ('跳', 'tiào', 0),
    ('亮', 'liàng', 0),
    ('《改变历史的战争》', '《 gǎi biàn lì shǐ de zhàn zhēng 》', 1),
    ('历史', 'lì shǐ', 1),
    ('冒险', 'mào xiǎn', 1),
    ('改变', 'gǎi biàn', 1),
    ('战争', 'zhàn zhēng', 1),
]


# 1. groups
# 2. select ques
# 3. build ques
# 4. return ques

groups = collections.defaultdict(list)

for idx, q in enumerate(ques):
    groups[q[2]].append(idx)

# print(groups)

#items = [x for x in range(len(ques))]
#print(items)

# select n from ques

#def random_items(lst, n = None):
#    if n is None or n > len(lst) or n < 0:
#        n = len(lst)
#    items = [x for x in range(len(lst))]
#    sel = []
#    for i in range(n):
#        idx = int(random.random() * (len(items) - i))
#        sel.append(items[idx])
#        items[idx] = items[len(items) - 1 - i]
#    return [lst[x] for x in sel]
#
#
#def random_item_idxs(lst, n = None):
#    if n is None or n > len(lst) or n < 0:
#        n = len(lst)
#    items = [x for x in range(len(lst))]
#    sel = []
#    for i in range(n):
#        idx = int(random.random() * (len(items) - i))
#        sel.append(items[idx])
#        items[idx] = items[len(items) - 1 - i]
#    return [[x, lst[x]] for x in sel]

#n = len(ques)
#sel = []
#for i in range(n):
#    idx = int(random.random() * (len(items) - i))
#    sel.append(items[idx])
#    items[idx] = items[len(items) - 1 - i]

def generate_questions(n = 10, swap = False, option_count = 4, questions = None, groups = None):

    assert questions is not None
    assert groups is not None
    


    sel = random_item_idxs(questions, n)

    #print(sel)

    result = []

    for item in sel:
        r = []

        ques_idx, ques_item = item
        
        title = ques_item[0]
        opt = ques_item[1]
        group = ques_item[2]

        group_items = groups[group] # 选中同组元素
        group_items.remove(ques_idx) # 删除正确答案

        if len(group_items) < 3: # 组内不够 3 个元素
            for g_idx, g in groups.items():
                if g_idx != group and len(g) > 3:
                    group_items += list(g)

        wrong_items = random_items(group_items, 3) # 选 3 个错误答案
        opt_items = random_items([ques_idx] + wrong_items)
        correct_idx = opt_items.index(ques_idx)

        r.append(title) # 添加 title
        for i in opt_items:
            r.append(questions[i][1]) # 添加选项
        r.append(correct_idx)

        assert len(r) == 6

        result.append(r)


        # item = ques[item_idx]
        # r.append(item[0]) # ques
        # group = item[2] # group

    return result


def gen_ques(n, swap = False, option_count = 4):
    return questions.generate_questions(n, swap, option_count, ques, groups)

